/*
	Global
*/
var Lives = new Array();
var getLive = new GetLiveInfo( function(xml){makeLive(xml);} );

/*
	Main
*/

$(document).ready(function(){
	getLive.nowPageRSSDownload();
});

// getLiveで取得した値を元に、live classを作成
function makeLive(xml){
    
	$(xml).find('item').each(function(){
		
		var community_id = $(this).find("community_id").text();
		var rObj = new RegExp("\\b"+community_id+"\\b","i");
        
		if ( typeof ( widget ) != 'undefined' ){
			if ( widget.preferenceForKey("fv_community").search(rObj) != -1  && community_id != '' ){
				
				var guid = $(this).find("guid").text();
				if ( Lives[guid] == undefined ){
					Lives[guid] = new Live(this);
					growlPost(Lives[guid].liveInfo);
				}else{
					Lives[guid].exportLiveDataToHTML();
					Lives[guid].lastCheckDate = new Date();
				}
			}
			
		}
		
	});
	
}

// growlPost(title, comment)
// Growlへの通知を行います。

function growlPost(liveInfo){
	
	var title = textEscape(liveInfo.title);
	var comment = textEscape(liveInfo.description);
	var url = 'http://icon.nicovideo.jp/'+liveInfo.type+'/'+liveInfo.community_id+'.jpg?';
	var guid = liveInfo.guid;
	
	
	setTimeout( function(){ widget.system("mkdir -p /private/tmp/n2lclite/;curl -o /private/tmp/n2lclite/"+guid+".jpg "+url+"; /usr/bin/osascript ./OriginalParts/growl.scpt "+title+" "+comment+" "+"/private/tmp/n2lclite/"+guid+".jpg"); }, 100 );
	
}

//エスケープ対象の文字列
function textEscape( text ){
	text = text.replace(/(!|\?|#|\$|%|&|\*|\[|\]|=|;|<|>|~|`|\\|\(|\s|\)|\||"|'|\|)/g,"\\$1");
	
	return text
}

//
// Function: refreshScrollArea()
// Updates the content areas's scroll bar when the content or article length has changed.
//
function refreshScrollArea(){
	var contentarea = document.getElementById("scrollArea");
	if (contentarea) setTimeout( function(){contentarea.object.refresh();}, 100 );
}